package br.unifor.mia.sd.proxy;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class HttpConnection {

	Socket socks = null;

	public HttpConnection(Socket socket) {
		socks = socket;
	}

	public void send(byte[] msg) throws IOException {
		OutputStream out = socks.getOutputStream();
		out.write(msg);
	}

	public byte[] receive() throws IOException {
		InputStream in = socks.getInputStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int size = 8192;
		byte[] chunk = new byte[size];
		int bytesRead = 0;
		while ((bytesRead = in.read(chunk)) >= 0) {
				out.write(chunk, 0, bytesRead);
			chunk = new byte[size];
		}
		return out.toByteArray();
	}

}
